<?php

namespace App\Admin\Controllers;

use App\Models\DispatchRule;
use App\Models\Media as Medium;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;
use Illuminate\Http\Request;

class MediumController extends AdminController
{
    public function getMediaSource(Request $request)
    {
        $key = $request->get('q');
        $typeList = Medium::getMediaType();
        $children = collect($typeList)->pluck('children', 'key')->get($key);

        return response()->json(
            collect($children)->map(function ($item) {
                return [
                    'id' => $item['key'],
                    'text' => $item['name'],
                ];
            })->toArray()
        );


    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new Medium(), function (Grid $grid) {
            $grid->column('id')->sortable();
            $grid->column('name');
//            $grid->column('crm_media_source_type');
//            $grid->column('crm_media_source_type_text');
//            $grid->column('crm_media_source');
//            $grid->column('crm_media_source_text');
//            $grid->column('rule_type');
            $grid->column('rules', '规则')
                ->display(function ($val) {
                    return count($val) . '条规则';
                })
                ->label();
            $grid->column('created_at');
            $grid->column('updated_at')->sortable();

            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');

            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new Medium(), function (Show $show) {
            $show->field('id');
            $show->field('name');
            $show->field('crm_media_source_type');
            $show->field('crm_media_source_type_text');
            $show->field('crm_media_source');
            $show->field('crm_media_source_text');
            $show->field('rule_type');
            $show->field('rules');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new Medium(), function (Form $form) {
            $form->display('id');
            $form->text('name')->required();
            $typeList = Medium::getMediaType();

            $form->select('crm_media_source_type')
                ->options(collect($typeList)->pluck('name', 'key')->toArray())
                ->required()
                ->load('crm_media_source', admin_route("admin.api.media.children"));
            $form->select('crm_media_source')
                ->required();
            $form->hidden('crm_media_source_type_text');
//            $form->text('crm_media_source');
//            $form->text('crm_media_source_text');

            $form->radio('rule_type')
                ->options(DispatchRule::RULE_TYPE_OPTIONS)
                ->default(0);

            $form->table('rules', '规则', function (Form\NestedForm $form) {
                $form->select('field', '字段')
                    ->options(DispatchRule::CLUE_FIELDS)
                    ->required();
                $form->select('operator', '条件')->options(DispatchRule::OPERATOR)
                    ->default('like')
                    ->required();

                $form->text('value', '值')
                    ->required();
            })->default([
                [
                    'field' => '',
                    'operator' => '',
                    'value' => '',
                ]
            ]);

            $form->display('created_at');
            $form->display('updated_at');
        });
    }
}
